
package JET.SA;

import JET.Centers.ControlCenter;
import JET.Frames.DataModels.DataModelsFactory;
import JET.Scene;
import JET.SceneHelper;
import JET.SceneItem;
import JET.physics.RSpring;
import JET.physics.bodies.SpringBody;
import java.util.ArrayList;

/**
 *
 * @author 057Ry
 */
public class SA_RSpring {

    private static ControlCenter CC = ControlCenter.getInstance();

    ////////////////////////////////////////////////////////////////////////////
    //                          R S P R I N G                                 //
    ////////////////////////////////////////////////////////////////////////////

    public static void addRSpringToSpringBody(RSpring rSpring, SpringBody sb, boolean updateSceneTreeSelection) {
        sb.addRSpring(rSpring);
        if (updateSceneTreeSelection)
            DataModelsFactory.getSceneTreeModel().fireSpringBodyChanged(sb);
    }
    public static void removeRSpringFromSpringBody(RSpring rSpring, Scene scene, boolean updateSceneTreeSelection) {
//        SpringBody sb = scene.getEngine().getSpringBodyThatOwnRSpring(rSpring);
        SpringBody sb = SceneHelper.getSpringBodyThatOwnRSpring(scene, rSpring);

        if (sb != null) {
            sb.removeRSpring(rSpring);
            if (updateSceneTreeSelection)
                DataModelsFactory.getSceneTreeModel().fireSpringBodyChanged(sb);
        }
    }

    public static void addRSpringToSceneItem(RSpring rs, SceneItem si, boolean updateScemeItemTreeSelection) {
        si.getEngine().addRSpring(rs);
        if  (updateScemeItemTreeSelection)
        {
            // Do sth with SceneItem tree ...
        }
    }

    public static void removeRSpringFromSceneItem(RSpring rSpring, Scene scene, boolean updateSceneTreeSelection) {
        SceneItem si = SceneHelper.getSceneItemThatOwnRSpring(scene, rSpring);

        if (si != null) {
            SA_Selection.removeRSpringFromSelection(rSpring, scene, true);
            si.getEngine().rSpringList.remove(rSpring);
//            if (updateSceneTreeSelection)
//                DataModelsFactory.getSceneTreeModel().fireSpringBodyChanged(sb);
        }
    }

    public static void removeRSpringsFromSpringBody(ArrayList<RSpring> list, Scene scene, boolean updateSceneTreeSelection) {
        for(RSpring rs : list)
            SA_RSpring.removeRSpringFromSpringBody(rs, scene, updateSceneTreeSelection);

        if (updateSceneTreeSelection)
            DataModelsFactory.getSceneTreeModel().fireSpringBodyListChanged();
    }

    public static void removeRSpringsFromSceneItem(ArrayList<RSpring> rSprings, Scene scene, boolean updateSceneTreeSelection) {
        for(RSpring rs : rSprings)
            removeRSpringFromSceneItem(rs, scene, updateSceneTreeSelection);
    }

    public static void addRSpringListToSpringBody(ArrayList<RSpring> rSpringList, SpringBody sb, boolean updateSceneTreeSelection) {
        sb.addRSprings(rSpringList);
        if (updateSceneTreeSelection)
            DataModelsFactory.getSceneTreeModel().fireSpringBodyChanged(sb);
    }

    static public synchronized void addRSpringToEngineList(RSpring rSpring, Scene scene) {
        scene.getJETContext().addRSpring(rSpring);
    }

    public static synchronized void removeRSpringFromEngine(RSpring rSpring, Scene scene) {
        scene.getJETContext().removeRSpring(rSpring);
    }

    public static synchronized void removeRSpringsFromEngine(ArrayList<RSpring> list, Scene scene) {
        for(RSpring rs : list)
            scene.getJETContext().removeRSpring(rs);
    }
}
